<template>
<div>

  <div>
    <div style="padding: 10px 0">
      <el-input style="width: 200px" placeholder="请输入订单编号" suffix-icon="el-icon-search" v-model="num"></el-input>
      <el-input style="width: 200px" placeholder="请输入状态关键字" suffix-icon="el-icon-search" v-model="state"></el-input>
      <el-button plain class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button plain class="ml-5" type="warning" @click="reset">重置</el-button>
      <el-button plain class="ml-5" type="success" @click="nowOrder">今日预约</el-button>
    </div>

    <el-empty description="暂无预约" v-if="tableData.length === 0"></el-empty>
    <el-table :data="tableData" border stripe header-cell-class-name="headerBg" v-else
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="80" v-if="false"></el-table-column>
      <el-table-column type="index"
                       :index="this.indexStartNum"
                       label="序号"
                       width="100">
      </el-table-column>
      <el-table-column prop="user.nickname" label="用户"></el-table-column>
      <el-table-column prop="num" label="编号" width="120"></el-table-column>
      <el-table-column prop="stage.date" label="就诊日期"></el-table-column>
      <el-table-column prop="stage.timePeriod" label="就诊时段"></el-table-column>
      <el-table-column prop="state" label="挂号状态">
        <template slot-scope="scope">
          <div v-if="scope.row.state !== '已诊断'">{{scope.row.state}}</div>
          <div style="color: green; font-weight: bold" v-else>{{scope.row.state}}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="140px">
        <template slot-scope="scope">
          <el-button plain type="success" @click="handleInfo(scope.row)" v-if="scope.row.state === '已挂号'">诊断 <i class="el-icon-edit"></i></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>

</div>
</template>

<script>
export default {
  name: "Application",
  data() {
    return {
      indexStartNum: 1,
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      num: "",
      phy: localStorage.getItem("phy") ? JSON.parse(localStorage.getItem("phy")) : {},
      users: {},
      physicians: {},
      hospitals: {},
      departments: {},
      state: "",
      dialogFormVisible: false,
      form: {},
      multipleSelection: [],
      headerBg: 'headerBg'
    }
  },
  created() {
    //请求分页查询数据
    this.load()
    //let mydate = '2023-01-21'
    //console.log(this.$Time(mydate))
  },
  methods: {
    comp: function(date){
      console.log(date)
      return this.$Time(date)
    },
    findStage(){
      this.request.get("/stage/findStage", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          id: this.user.id
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data
        this.total = res.total
      })
    },
    nowOrder(){
      this.request.get("/order/nowPhyOrder", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          num: this.num,
          state: this.state,
          id: this.phy.id
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data
        this.total = res.total
      })
    },
    load() {
      this.request.get("/order/phyOrder", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          num: this.num,
          state: this.state,
          id: this.phy.id
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data
        this.total = res.total
        /*let myDate = res.data.stage.date
        console.log("myDate"+myDate)
        let result = this.$Time(myDate)
        console.log(result)*/
      })
    },
    save() {
      this.request.post("/order/edit", this.form).then(res => {
        if (res) {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleInfo(row) {
      this.$confirm('确定已经诊断吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        row.state = '已诊断'
        console.log("诊断信息")
        console.log(row)
        this.request.post("/order/edit", row).then(res => {
          if (res) {
            this.$message.success("保存成功")
            this.load()
          } else {
            this.$message.error("保存失败")
          }
        })
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row) {
      this.form = Object.assign({}, row)
      this.dialogFormVisible = true
    },
    del(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/order/del?id=" + id).then(res => {
          if (res) {
            this.$message.success("删除成功")
            this.load()
          } else {
            this.$message.error("删除失败")
          }
        })
      });
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    deleteBatch() {
      let ids = this.multipleSelection.map(v => v.id) //将对象数组变成纯id数组 [{},{},{}] => [1,2,3]
      this.$confirm('此操作将永久批量删除选中数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/order/delBatch", {data: ids}).then(res => {
          if (res) {
            this.$message.success("批量删除成功")
            this.load()
          } else {
            this.$message.error("批量删除失败")
          }
        })
      });
    },
    reset() {
      this.num = ""
      this.state = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    getDate() {
      const date = new Date();
      const seperator1 = "-";
      const year = date.getFullYear();
      let month = date.getMonth() + 1;
      let strDate = date.getDate();

      if (month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
      }
      return year + " - " + month + " - " + strDate;
    }
  }
}
</script>

<style scoped>

</style>
